<?php

# SIMS-WB
# Sistem Informasi Manajemen Siswa - Wisma Bahasa Yogyakarta
# 
# version 1.0
# class authlogin
# file models/authlogin.php
# created Oct 30, 2012 10:37:25 AM
# 
# (c)2012, arif.laksito@gmail.com

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Authlogin extends SI_Model {

    /**
     * variabel penyimpanan sementara untuk output login
     * @var array
     */
    var $usr_data = array();

    /**
     * Main Login function
     *
     * @param char/int $usr
     * @param char $pwd
     * @return boolean
     */
    public function login($usr, $pwd) {

        $rules = array(
            array(
                'field' => 'usr',
                'label' => 'Username',
                'rules' => 'required|alpha_dash'
            ),
            array(
                'field' => 'pwd',
                'label' => 'Password',
                'rules' => 'required'
            )
        );

        if (!$this->set_field($rules)) {
            $this->umsg = $this->msg;
            return false;
        }


        if ($this->_login($usr, $pwd)) {
            return true;
        } else {

            $this->umsg = 'Login tidak berhasil, silahkan cek kembali user dan password anda';
            return false;
        }
    }

    /**
     * login for admin, success set userData variable.
     * usr_data['level'] = 1(admin)
     *
     * @param char/int $u
     * @param char $p
     * @return boolean
     */
    private function _login($u, $p) {
        $p = md5($p);
        
        $this->db->where('user_pengguna', $u);
        $this->db->where('pass_pengguna', $p);
        $this->db->where('status_pengguna', 1);        
        $query = $this->db->get('pengguna');
        
        $rs = $query->row_array();

        if ($this->db->affected_rows() > 0) {
            $this->usr_data['id'] = $rs['id_pengguna'];
            $this->usr_data['name'] = $rs['nama_lengkap'];
            $this->usr_data['log'] = $rs['tanggal_log'];
            $this->usr_data['level'] = $rs['level_pengguna'];

            $this->update_log($rs['id_pengguna']);

            return true;
        } else {
            return false;
        }
    }

    /**
     * update date log 
     */
    private function update_log($id) {
        $str = "Update pengguna Set tanggal_log=now() Where id_pengguna=$id";
        $query = $this->db->query($str);

        if ($this->db->affected_rows() > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * set session/cookie for login page
     */
    public function set_cookies() {
        $this->session->set_userdata($this->usr_data);
    }

    /**
     * get session/cookie value
     * @return array
     */
    public function get_cookies() {
        $sess = array();

        $sess['id'] = $this->session->userdata('id');
        $sess['name'] = $this->session->userdata('name');
        $sess['log'] = $this->session->userdata('log');
        $sess['level'] = $this->session->userdata('level');

        return $sess;
    }

    /**
     * check valid session/cookie in page
     * @return boolean
     */
    public function valid_cookies() {
        if ($this->session->userdata('id') != '')
            return true;
        else
            return false;
    }

    /**
     * delete session/cookie value
     */
    public function del_cookies() {
        $sess = array();

        $sess['id'] = '';
        $sess['name'] = '';
        $sess['log'] = '';
        $sess['level'] = '';

        $this->session->unset_userdata($sess);
        $this->session->sess_destroy();
    }

}